package com.myTimeUtils.android.TimeTracker.Schedule;
import com.myTimeUtils.android.TimeTracker.Global.Iterator;
import java.util.ArrayList;
import java.util.Collections;
/**
 * Models a day and constructs an iterator over the periods in the day.
 *
 * @author Joseph Walker
 */
public class Day{
	private ArrayList<Period> periods;
	/**
	 * Initializes a day object.
	 */
	public Day(){
		this.init();
	}
	/**
	 * Adds a period to the given day. If the period overlaps a
	 * period currently in the list it will not be added.
	 *
	 * @param period			The period to add
	 * @return 				True if the period can be added
	 */
	@SuppressWarnings("unchecked")
	public boolean addPeriod(Period period){
		for(Period p : periods){
			if(p.equals(period)){
				return false;
			}
		}
		this.periods.add(period);
		Collections.sort(periods);
		return true;
	}
	/**
	 * Constructs an iterator over this list of periods.
	 *
	 * @return		An iterator over the periods
	 */
	public Iterator<Period> getIterator(){
		return new Iterator<Period>(){
			private ArrayList<Period> element = periods;
			private int elementNum = 0;
			public void clear(){ this.elementNum = 0; }
			public Period current(){ return this.element.get(this.elementNum); }
			public boolean hasNext(){ return this.elementNum < this.element.size(); }
			public void next(){ this.elementNum++; }
		}; 
	}
	/**
	 * Returns the number of periods in this day.
	 *
	 * @return			The number of periods in this day
	 */
	public int getNumPeriods(){ return this.periods.size(); }
	/**
	 * Returns the specified period from this day.
	 *
	 * @param period		The period number to return
	 * @return 			The specified period
	 */
	public Period getPeriod(int period){
		return this.periods.get(period);
	}
	private void init(){ this.periods = new ArrayList<Period>(); }
	/**
	 * Removes the given period from the list.
	 *
	 * @param period	The period to remove from the list
	 * @return		True if the period is removed
	 */
	@SuppressWarnings("unchecked")
	public boolean removePeriod(Period period){
		boolean out = this.periods.remove(period);
		if(out) Collections.sort(periods);
		return out;
	}
}
